package com.example.ecommercemanagesystem.service.impl;


import com.example.ecommercemanagesystem.dao.GoodsDao;
import com.example.ecommercemanagesystem.dto.GoodsDto2;
import com.example.ecommercemanagesystem.entity.Goods;
import com.example.ecommercemanagesystem.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }


    @Override
    public boolean insertGoods(Goods goodsDto) {
        return goodsDao.addGoods(goodsDto);
    }

    @Override
    public Page<GoodsDto2> queryGoods(GoodsDto2 goods, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<GoodsDto2> list=goodsDao.queryGoods(goods);
        return list;
    }

    @Override
    public Long queryGoodsCount(GoodsDto2 goods) {
        return goodsDao.queryGoodsCount(goods);
    }

    @Override
    public GoodsDto2 getGoodsById(Integer id) {
        return goodsDao.getGoodsById(id);
    }

    @Override
    public boolean updateGoods(GoodsDto2 goodsDto2) {
        return goodsDao.updateGoods(goodsDto2);
    }

    @Override
    public HSSFWorkbook exportExcel(Map<String, Object> map) {
        //获取需要生成excel的数据
        List<GoodsDto2> list = goodsDao.getAllGoods(map);
        //判断是否为空
        if (list == null) {
            return null;
        }

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //创建一个excel的sheet
        HSSFSheet sheet = hssfWorkbook.createSheet("firstshett");
        //表头属性名
        String[] propertyName = {"序号", "商品名称", "商品价钱", "商品类型", "商品单位", "商品库存", "上架时间", "商品描述", "商品介绍"};
        //获取表所需要的数量
        int length = propertyName.length;
        //-------设置第一行---------
        //设置行下面为第一行
        HSSFRow row = sheet.createRow(0);
        //设置列下面为第一行的第一列
        HSSFCell hssfCell = row.createCell(0);


        //设置字体
        HSSFFont hssfFont = hssfWorkbook.createFont();
        hssfFont.setFontName("黑体");
        //字体大小
        hssfFont.setFontHeightInPoints((short) 12);
        //设置样式
        HSSFCellStyle hssfCellStyle = hssfWorkbook.createCellStyle();
        //使用上面的字体样式
        hssfCellStyle.setFont(hssfFont);
        hssfCellStyle.setLocked(true);
        //自动换行
        hssfCellStyle.setWrapText(false);
        CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, length - 1);
        //把合并单元格设置进去
        sheet.addMergedRegion(rangeAddress);
        //设置样式
        hssfCell.setCellStyle(hssfCellStyle);
        hssfCell.setCellValue("车辆列表信息");
        //----------设置第二行-----------
        HSSFRow row2 = sheet.createRow(1);
        //字段名的字体
        HSSFFont hssfFont1 = hssfWorkbook.createFont();
        hssfFont1.setFontName("宋体");
        //字体大小
        hssfFont1.setFontHeightInPoints((short) 12);
        //列表样式
        HSSFCellStyle hssfCellStyle1 = hssfWorkbook.createCellStyle();
        //设置字体字体样式
        hssfCellStyle1.setFont(hssfFont1);
        hssfCellStyle1.setLocked(true);
        hssfCellStyle1.setWrapText(false);

        //设置行高
        row2.setHeight((short) 750);
        //创建列
        HSSFCell cell = null;
        for (int i = 0; i < length; i++) {
            cell = row2.createCell(i);
            //设置字段名
            cell.setCellValue(propertyName[i]);
            //设置样式
            cell.setCellStyle(hssfCellStyle1);
            //----设置表里面的内容--------------
            //设置字体
            HSSFFont font=hssfWorkbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short)10);
            //设置样式
            HSSFCellStyle hssfCellStyle2=hssfWorkbook.createCellStyle();
            hssfCellStyle2.setFont(font);
            hssfCellStyle2.setWrapText(true);
            HSSFRow hssfRow=null;
            HSSFCell hssfCell1=null;
            int valueStatrRow=2;
            //赋值
            for (GoodsDto2 goodsDto2:list
            ) {
                hssfRow=sheet.createRow(valueStatrRow);
                //id
                hssfCell1=hssfRow.createCell(0);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_id());
                //商品名称
                hssfCell1=hssfRow.createCell(1);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_name());
                //商品价钱
                hssfCell1=hssfRow.createCell(2);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_price());
                //商品类型
                hssfCell1=hssfRow.createCell(3);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getType_name());
                //商品单位
                hssfCell1=hssfRow.createCell(4);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_unit());
                //商品库存
                hssfCell1=hssfRow.createCell(5);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_number());
                //商品上架时间
                hssfCell1=hssfRow.createCell(6);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_time());
                //商品描述
                hssfCell1=hssfRow.createCell(7);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_info());
                //商品介绍
                hssfCell1=hssfRow.createCell(8);
                hssfCell.setCellStyle(hssfCellStyle2);
                hssfCell1.setCellValue(goodsDto2.getGoods_introduce());
                valueStatrRow++;



            }

        }
        return hssfWorkbook;
    }
}
